package com.lzy.crm.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.lzy.crm.base.mybatis.BaseMapper;
import com.lzy.crm.base.page.PageQuery;
import com.lzy.crm.base.service.BaseServiceImpl;
import com.lzy.crm.base.util.JsonUtil;
import com.lzy.crm.dao.BankCardMapper;
import com.lzy.crm.dao.ProfitMapper;
import com.lzy.crm.dao.TradeFlowMapper;
import com.lzy.crm.dao.criteria.ProfitCriteria;
import com.lzy.crm.domain.BankCard;
import com.lzy.crm.domain.Profit;
import com.lzy.crm.domain.TradeFlow;
import com.lzy.crm.domain.User;
import com.lzy.crm.service.BankCardService;
import com.lzy.crm.service.ProfitService;
import com.lzy.crm.service.UserService;
import com.lzy.crm.vo.PpUserVo;
import com.lzy.crm.vo.ProfitVo;

@Service("profitService")
public class ProfitServiceImpl extends BaseServiceImpl<Profit,ProfitCriteria> implements ProfitService {

	@Resource
	ProfitMapper profitMapper;
	
	@Resource
	UserService userService;
	
	@Resource
	BankCardService bankCardService;
	
	@Resource
	TradeFlowMapper tradeFlowMapper;
	
	@Override
	public BaseMapper<Profit, ProfitCriteria> getBaseMapper() {
		return profitMapper;
	}

	
	/**
	 * 查询分成明细
	 * @param userId
	 * @param pageQuery
	 * @return
	 */
	public List<ProfitVo> getProfit(Integer puId,String uid,PageQuery pageQuery){
		
		List<ProfitVo>  profitVoList = new ArrayList<ProfitVo>();
		List<Profit> profitList= profitMapper.getProfit(puId,uid, pageQuery);
		for (Profit profit :profitList) {
			ProfitVo profitVo = JsonUtil.fromJson(JsonUtil.toJson(profit), ProfitVo.class);
			
			profitVoList.add(profitVo);
		}
		
		return profitVoList;
	}
	
	
	/**
	 * 统计时间段总分润数
	 * @param parentId
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public Integer getProfitStat(Integer parentId,String startTime,String endTime){
		return profitMapper.getProfitStat(parentId, startTime, endTime);
	}

	
	/**
	 * 查询打过的局数
	 * @param userId
	 * @return
	 */
	@Override
	public Integer getProfitGameCount(Integer userId,String startTime,String outStartTime,String outEndTime){
		return profitMapper.getProfitGameCount( userId, startTime, outStartTime, outEndTime);
		
	}
	
	/**
	 *申请体现功能
	 * @param user
	 * @return
	 */
	public Integer player_kiting(User user){
		Integer result = 0;
		List<BankCard> bankCardList = bankCardService.getBankCardListByUserId(user.getId().toString());
		if(bankCardList==null||bankCardList.size()==0){
			// 打款信息不全
			return 1;
		}
		/*BankCard bankCard = bankCardList.get(0);
		PpUser playerUser = userService.getById(user.getId());
		// 扣取打款的金额
		double newAmount = playerUser.getTiceng() ;
		if(newAmount<1){
			//提款金额要大于1元
			return 2;
		}
		playerUser.setTiceng(0.0);
		userService.updateByPrimaryKeySelective(playerUser);
		// 生成打款记录信息
		TradeFlow tradeFlow =new TradeFlow();
		tradeFlow.setUserId(playerUser.getId());
		tradeFlow.setGameUserId(playerUser.getGameUserId());
		tradeFlow.setAmount(newAmount);
		tradeFlow.setBankName(bankCard.getBankName());
		tradeFlow.setBankUserName(bankCard.getBankUserName());
		tradeFlow.setBankUserNo(bankCard.getBankUserNo());
		tradeFlow.setStatus("0");
		tradeFlow.setCreateTime(new Date());
		tradeFlow.setCreateUserId(user.getId());
		tradeFlow.setRemark("红包入口体现申请");
		tradeFlowMapper.insertSelective(tradeFlow);*/
		return result;
		
	}
	
	
}
